package com.code4lazy.controller;
import com.code4lazy.pojo.Admins;

import com.code4lazy.service.AdminService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.servlet.ModelAndView;
import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Enumeration;

/**
 * @author geekforfree
 * DATE   2018/3/16
 */

@RestController
public class AdminController {

    @Resource
    AdminService adminService;

    @RequestMapping(value = "/" ,method = RequestMethod.GET)
    public ModelAndView Login(){
        ModelAndView mv = new ModelAndView("login");
        return mv;
    }

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("index");
        return mv;
    }

//    @RequestMapping(value = "admin/login" ,method = RequestMethod.POST)
//    public ModelAndView sumbit(Admins admin, HttpServletRequest request, HttpSession session, Model model)
//    {
//        Admins adminSql = this.adminService.selectByName(admin.getUserName());
//        // 找到数据
////        ModelAndView mv = new ModelAndView();
//        if(adminSql != null) {
//             if(adminSql.getUserName().equals((admin.getUserName()))&&(adminSql.getpWd().equals(admin.getpWd()))){
//                 // 设置 sessionid
//                 session.setAttribute("userid",adminSql.getAdminId());
////                 mv.addObject("admin",admin);
////                 mv.setViewName("index");
////                 model.addAttribute("admin",admin);
//                 session.setAttribute("admin",admin);
//                 return new ModelAndView ("redirect:/index");
//             }
//             else{
////                 mv.addObject("msg","用户名或密码不正确");
////                 model.addAttribute("msg","用户名或密码不正确");
//                 session.setAttribute("msg","用户名或密码不正确");
//                 return new ModelAndView("redirect:/","msg","用户名或密码不正确");
//             }
//        }else{
////            mv.addObject("msg","用户名不存在");
////            model.addAttribute("msg","用户名不存在");
//            session.setAttribute("msg","用户名不存在");
//            return new ModelAndView("redirect:/","msg","用户名不存在");
//        }
////        return mv;
//
//
//    }
//
@RequestMapping(value = "admin/login",method = RequestMethod.POST)
public ModelAndView submit(@RequestParam String userName, @RequestParam String pWd,HttpSession session){
    Subject subject = SecurityUtils.getSubject();
    //
    String salt = userName;

    String md5pWd = new Md5Hash(pWd,salt).toString();

    UsernamePasswordToken token = new UsernamePasswordToken(userName,md5pWd);

    try{
        subject.login(token);
        session.setAttribute("userName",userName);
        return new ModelAndView("redirect:/index");
    }catch (AuthenticationException e){
        session.setAttribute("msg","登录失败");
    }
    return new ModelAndView("redirect:/");

}
//@RequestMapping(value = "admin/logout",method = RequestMethod.GET)
//public ModelAndView logout(HttpSession session){
//
//    Enumeration<String> em = session.getAttributeNames();
//    while(em.hasMoreElements()){
//        session.removeAttribute(em.nextElement().toString());
//    }
//    return new ModelAndView("redirect:/","msg","已退出");
//}
    @RequestMapping(value = "/logout")
    public  ModelAndView logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return new ModelAndView("redirect:/");
    }
}
